home *** CD-ROM | disk | FTP | other *** search
/ DOpus Plus / DOpus Plus.iso / Tutorial / C Guide / Simple_Module2 / NotifyWindow / NotifyWindow.c < prev    next >
C/C++ Source or Header  |  1998-08-26  |  13KB  |  226 lines

  1. /*******************************************************************
  2.  
  3.    NotifyWindow.c
  4.         
  5.         Does open a simple window with some easy gadgets and installs
  6.         also notification for events:
  7.         - DOpus quit
  8.         - DOpus hide
  9.         - DOpus show
  10.         - Diskchange    
  11.                                 
  12. *********************************************************************/
  13.  
  14. #define PARENT  
  15. #include "/includes/Window.h"
  16.  
  17. /********************************************************************/
  18. // locale prototypes
  19.  
  20. BOOL OpenDOpusWin( WindowHandle *wh );
  21.  
  22. BOOL HandleWindow( WindowHandle *wh );
  23.  
  24. BOOL InstallNotify( WindowHandle *wh ); 
  25. BOOL HandleNotify( WindowHandle *wh );
  26.  
  27. /********************************************************************/
  28.  
  29. void OwnWindow( STRPTR args, struct Screen *screen )
  30. {
  31.         WindowHandle          *wh;
  32.         
  33.         if( (wh = AllocMemH(mempool, sizeof(WindowHandle))) ) // allocate some memory
  34.           {
  35.              wh->screen = screen;     // store the screen pointer
  36.                   
  37.              // now the change
  38.                   
  39.              if( InstallNotify(wh) )
  40.                {
  41.                   if( OpenDOpusWin(wh) )   // open the window
  42.                     {
  43.                        // we copy now the arguments into the text gadget
  44.                        // if we have supplied some...
  45.                                  
  46.                        SetGadgetValue( wh->olist, GADGET_ID_TEXT, (ULONG) args );
  47.                                                                                                  
  48.                        while( TRUE ) 
  49.                          {
  50.                              wh->signals = Wait( (wh->win ? 1 << wh->win->UserPort->mp_SigBit : NULL) |   // wait for window events if window is open
  51.                                                  1 << wh->notify_port->mp_SigBit  );
  52.                                                           
  53.                              if( wh->signals & 1 << wh->win->UserPort->mp_SigBit )
  54.                                   if( HandleWindow(wh) )
  55.                                        break;              // does end the while loop
  56.                                                           
  57.                              if( wh->signals & 1 << wh->notify_port->mp_SigBit )
  58.                                   if( HandleNotify(wh) )
  59.                                        break;                                                       
  60.                                    
  61.                           }
  62.                                  
  63.                                                 if( wh->win ) // again a small change !
  64.                                  CloseConfigWindow( wh->win );
  65.                                         }
  66.                                         
  67.                             RemoveNotifyRequest( wh->notify_handle ); // remove our request
  68.                                  
  69.                                  while( !IsMsgPortEmpty(wh->notify_port) ) // clear the port
  70.                                       ReplyFreeMsg( GetMsg(wh->notify_port) );
  71.                                                 
  72.                                  DeleteMsgPort( wh->notify_port ); // and delete him
  73.                          }
  74.                          
  75.                   FreeMemH( wh ); // free our memory
  76.           }                                               
  77. }
  78.  
  79. /********************************************************************/
  80. // This function does open our window. We could have done this in the
  81. // function OwnWindow() too, but we need this function later again.
  82.  
  83. BOOL OpenDOpusWin( WindowHandle *wh )
  84. {
  85.         NewConfigWindow ncfgwin;     // we need a NewConfigWindow structure too
  86.                                      // of couse you could also allocate it with
  87.                                                                                   // AllocMemH()...
  88.         
  89.         // and have to fill it
  90.         
  91.         ncfgwin.nw_Parent = wh->screen;  // open on this screen
  92.         
  93.         // getting a localized title...
  94.         ncfgwin.nw_Title  = DOpusGetString( locale, MSG_WINDOW_TITLE );
  95.         
  96.         ncfgwin.nw_Dims   = &cfgwin; // a pointer to the ConfigWin structure
  97.         ncfgwin.nw_Locale = locale;  // the module locale pointer (from modinit.c)
  98.         ncfgwin.nw_Port   = NULL;    // we doesn't supply a port
  99.         ncfgwin.nw_Font   = NULL;    // just taking the screen font
  100.         ncfgwin.nw_Flags  = WINDOW_REQ_FILL      | // fill with stripple pattern
  101.                             WINDOW_AUTO_KEYS     | // handle keys automatic
  102.                                                           WINDOW_SCREEN_PARENT;  // nw_Parent points to a screen
  103.         
  104.         if( (wh->win = OpenConfigWindow(&ncfgwin)) )   // open the window
  105.           {
  106.                   if( (wh->olist = AddObjectList(wh->win, odef)) ) // add the gadgets
  107.                          return TRUE;
  108.                                         
  109.                   CloseConfigWindow( wh->win ); //      in error case do not forget :-)
  110.           }
  111.         
  112.         return FALSE;
  113. }
  114.  
  115.  
  116. /********************************************************************/
  117. // we does only close the window, if the closegadget was pressed
  118. // if you want to close it within a gadget, you must only in the
  119. // right case set "stop" to TRUE
  120.  
  121. BOOL HandleWindow( WindowHandle *wh )
  122. {
  123.          BOOL stop = FALSE;
  124.          ULONG value;
  125.          
  126.          while( !stop && (wh->imsg = GetWindowMsg(wh->win->UserPort)) )
  127.                 {
  128.                         switch( wh->imsg->Class )  // let's handle the IDCMP
  129.                           {
  130.                                   case IDCMP_GADGETUP:
  131.                                                           switch( GET_ID(wh->imsg) )
  132.                                              {
  133.                                                                                                                  case GADGET_ID_CYCLE:  // we copy simply the same text to the text gadget
  134.                         
  135.                                                                                  value = GetGadgetValue( wh->olist, GADGET_ID_CYCLE ) + MSG_CLICK_ME;
  136.                                                                                                                     SetGadgetValue( wh->olist, GADGET_ID_TEXT, (ULONG) DOpusGetString(locale, value) );
  137.                                                                                                                     break;
  138.                         
  139.                                                           case GADGET_ID_OKAY:   // doing a message
  140.                                                                                  
  141.                                                                                                                                                                                 SetGadgetValue( wh->olist, GADGET_ID_TEXT, (ULONG) DOpusGetString(locale, MSG_OKAY_DONE) );
  142.                                                                                                                     break;
  143.                                                                                                         
  144.                                                           case GADGET_ID_CANCEL: 
  145.                                                                                  SetGadgetValue( wh->olist, GADGET_ID_TEXT, (ULONG) DOpusGetString(locale, MSG_CANCEL_DONE) );
  146.                                                                                                                     break;
  147.                                                   }
  148.                                                                                                                                  
  149.                                                                                                   break;
  150.                                                                          
  151.                                   case IDCMP_CLOSEWINDOW:
  152.                                                           // we can not simply return here, the IntuiMessage must replied first
  153.                                                                                                   
  154.                                                                                    stop = TRUE;
  155.                                                                                                   break;                                                                         
  156.                           }
  157.                                                                  
  158.                         ReplyWindowMsg( wh->imsg );  
  159.                                                           
  160.                         // remember: You should not use any other routines
  161.                         // to get/reply the messages of this window than
  162.                         // GetWindowMsg() and ReplyWindowMsg() !!
  163.            }
  164.                                                          
  165.     return stop;
  166. }
  167.  
  168. /********************************************************************/
  169.  
  170. BOOL InstallNotify( WindowHandle *wh )
  171. {
  172.           if( (wh->notify_port = CreateMsgPort()) ) // a private port is enough
  173.             {
  174.                          if( (wh->notify_handle = AddNotifyRequest( DN_OPUS_HIDE | DN_OPUS_QUIT | DN_OPUS_SHOW | DN_DISKCHANGE,
  175.                                                                     NULL, wh->notify_port)) )
  176.                               return TRUE;
  177.                          
  178.                          DeleteMsgPort( wh->notify_port );
  179.                  }
  180.                  
  181.           return FALSE; 
  182. }
  183.  
  184. BOOL HandleNotify( WindowHandle *wh )
  185. {
  186.           BOOL ret_value = FALSE;
  187.  
  188.           
  189.           while( !ret_value && (wh->notify_msg = (DOpusNotify *) GetMsg(wh->notify_port)) )
  190.             {
  191.                          switch( wh->notify_msg->dn_Type )
  192.                            {
  193.                                         case DN_OPUS_QUIT:  // we should quit
  194.                                                            
  195.                                                                                           ret_value = TRUE;
  196.                                                                                           break;
  197.                                         
  198.                                         case DN_OPUS_HIDE:  // we must close our window
  199.                                                            
  200.                                                                                           CloseConfigWindow( wh->win );
  201.                                                                                           wh->win = NULL;  // do not forget this ! It's a kind of flag...
  202.                                                                                           break;
  203.                                         
  204.                                         case DN_OPUS_SHOW:  // we can reopen our window, if it was closed
  205.                                                            
  206.                                                                                           if( !wh->win )
  207.                                                                  OpenDOpusWin( wh );
  208.                                                                                           break;
  209.                                         
  210.                                         case DN_DISKCHANGE: // we set the value of our text gadget
  211.                                         
  212.                                                             if( wh->win )
  213.                                                                                             {           
  214.                                                                                                          sprintf( wh->buffer, "Diskchange in drive %s", wh->notify_msg->dn_Name );
  215.                                                                                                          SetGadgetValue( wh->olist, GADGET_ID_TEXT, (ULONG) wh->buffer );
  216.                                                                                                  }
  217.                                                                                      
  218.                                                                                           break;                                        
  219.                                 }
  220.                          
  221.                          ReplyFreeMsg( (struct Message *) wh->notify_msg );
  222.                  }
  223.                  
  224.           return ret_value;       
  225. }
  226.